﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CADTool.Game
{
    public class Game
    {
        public Flash _circle;
        public Flash _flash;
        public DBText dBText;
        Timer _timer;
        Vector3d _vect; 
        [CommandMethod("DZKYX")]
        public void DZKYX()
        {
            Polyline bk = new Polyline();
            bk.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
            bk.AddVertexAt(1, new Point2d(100, 0), 0, 0, 0);
            bk.AddVertexAt(2, new Point2d(100, 150), 0, 0, 0);
            bk.AddVertexAt(3, new Point2d(0, 150), 0, 0, 0);
            bk.Closed = true;
            CustomMethod.AddEntities(new List<Entity> { bk });
            DBText score = new DBText()
            {
                TextString = "分数",
                Height = 5,
                Position = new Point3d(101, 145, 0),
            };
            dBText = new DBText()
            {
                TextString = "0",
                Height = 5,
                Position = new Point3d(score.Bounds.Value.MaxPoint.X + 1, 145, 0)
            };
            CustomMethod.AddEntities(new List<Entity> { score });
            Move.Polylines = new List<Polyline>();
            Polyline pl = new Polyline();
            pl.AddVertexAt(0, new Point2d(2, 148), 0, 0, 0);
            pl.AddVertexAt(1, new Point2d(2, 148), 0, 0, 0);
            pl.AddVertexAt(2, new Point2d(2, 148), 0, 0, 0);
            pl.AddVertexAt(3, new Point2d(2, 148), 0, 0, 0);
            pl.Closed = true;
            for (int i = 0; i < 5; i++)
            {
                Polyline pline = pl.Clone() as Polyline;
                Vector3d vector3D = new Vector3d(20 * i, -5 * i, 0);
                Matrix3d matrix3D = Matrix3d.Displacement(vector3D);
                pline.TransformBy(matrix3D);
                Move.Polylines.Add(pline);
                CustomMethod.AddEntities(new List<Entity> { pline });
            }
            _vect = new Vector3d(2, 2, 0);
            Circle circle = new Circle()
            {
                Radius = 5,
                ColorIndex = 1,
                Center = new Point3d(50, 25, 0)
            };
            _circle = new Flash(circle);
            _timer = new Timer();
            _timer.Interval = 1;
            _timer.Tick += _timer_Tick;
            _timer.Start();
            ZKJig zKJig = new ZKJig();
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            PromptResult pr = ed.Drag(zKJig);
            _timer.Stop();
            _circle.FlashErase();
        }
        private void _timer_Tick(object sender, EventArgs e)
        {
            Circle circle = _circle._ent as Circle;
            circle.Center += _vect;
            if (circle.Center.X > 95)
            {
                circle.Center = new Point3d(190 - circle.Center.X, circle.Center.Y, 0);
                _vect = new Vector3d(-_vect.X, _vect.Y, 0);
            }
            if (circle.Center.X < 5)
            {
                circle.Center = new Point3d(10 - circle.Center.X, circle.Center.Y, 0);
                _vect = new Vector3d(-_vect.X, _vect.Y, 0);
            }
            if (circle.Center.X > 145)
            {
                circle.Center = new Point3d(circle.Center.X, 220 - circle.Center.Y, 0);
                _vect = new Vector3d(-_vect.X, _vect.Y, 0);
            }
            Polyline polyline = null;
            foreach (var pl in Move.Polylines)
            {
                Point3dCollection pos = new Point3dCollection();
                pl.IntersectWith(_circle._ent, Intersect.OnBothOperands, pos, IntPtr.Zero, IntPtr.Zero);
                if (pos.Count > 0)
                {
                    polyline = pl;
                    break;
                }
            }
            if (polyline != null)
            {
                int num = Convert.ToInt32(dBText.TextString);
                num++;
                using (Transaction trans = Z.db.TransactionManager.StartTransaction())
                {
                    dBText.Id.GetObject(OpenMode.ForWrite);
                    dBText.TextString = num.ToString();
                    trans.Commit();
                }
                circle.Center = new Point3d(circle.Center.X, circle.Center.Y * 2 - circle.Center.Y, 0);
                _vect = new Vector3d(_vect.X, _vect.Y, 0);
                Move.Polylines.Remove(polyline);
                CustomMethod.EraseEntities(new List<Entity> { polyline });
            }

            if (circle.Center.Y == 25)
            {
                if (circle.Center.X >= Move.Polyline.Bounds.Value.MinPoint.X && circle.Center.X <= Move.Polyline.Bounds.Value.MaxPoint.X)
                {
                    circle.Center = new Point3d(circle.Center.X, 50 - circle.Center.Y, 0);
                    _vect = new Vector3d(_vect.X, -_vect.Y, 0);
                }
            }
            _circle.FlashUpdate(circle);
            if (circle.Center.Y < 5)
            {
                _timer.Stop();
                _circle.FlashErase();
            }
        }
        public class ZKJig : DrawJig
        {
            double xx;
            Polyline polyline;
            public ZKJig()
            {
                xx = 50;
                polyline = new Polyline();
                polyline.AddVertexAt(0, new Point2d(50, 15), 0, 0, 0);
                polyline.AddVertexAt(1, new Point2d(60, 15), 0, 0, 0);
                polyline.AddVertexAt(2, new Point2d(60, 20), 0, 0, 0);
                polyline.AddVertexAt(3, new Point2d(40, 20), 0, 0, 0);
                polyline.AddVertexAt(4, new Point2d(40, 15), 0, 0, 0);
                polyline.AddVertexAt(5, new Point2d(50, 15), 0, 0, 0);
                Move.Polyline = polyline.Clone() as Polyline;
            }
            protected override SamplerStatus Sampler(JigPrompts prompts)
            {
                PromptPointResult psr = prompts.AcquirePoint();
                if (psr.Status == PromptStatus.OK)
                {
                    Point3d pttemp = psr.Value;
                    if (pttemp.X != xx)
                    {
                        double xxx = pttemp.X;
                        if (pttemp.X < 10) xxx = 10;
                        if (pttemp.X > 90) xxx = 90;
                        Vector3d vector3D = new Vector3d(xxx - xx, 0, 0);
                        Matrix3d matrix3D = Matrix3d.Displacement(vector3D);
                        Move.Polyline = polyline.Clone() as Polyline;
                        Move.Polyline.TransformBy(matrix3D);
                        return SamplerStatus.OK;
                    }
                }
                return SamplerStatus.NoChange;
            }

            protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
            {
                draw.Geometry.Draw(Move.Polyline);
                return true;
            }
        }


    }
}
